﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections.Specialized;
using System.Security.Cryptography.X509Certificates;
using System.IO;

namespace DataAccess
{
    public partial class DataAccess
    {
        public DataAccessResult UpdateDeploymentStatus(String SubscriptionID,String CertificatePath,String ServiceName,String DeploymentSlot,String Status)
        {
            DataAccessResult result = new DataAccessResult();
            string RequestBodyFormat = @"<?xml version=""1.0"" encoding=""utf-8""?>
<UpdateDeploymentStatus xmlns=""http://schemas.microsoft.com/windowsazure"">
  <Status>" + Status + "</Status></UpdateDeploymentStatus>";

            string requestUrl = "https://management.core.windows.net/" + SubscriptionID + "/services/hostedservices/"+ServiceName+"/deploymentslots/"+DeploymentSlot+"/?comp=status";
            String requestBody = RequestBodyFormat;
            string ReturnBody = string.Empty;
            WebResponse resp = null;
            try
            {
                NameValueCollection RequestHeaders = new NameValueCollection();
                RequestHeaders.Add("x-ms-version", x_ms_version);

                X509Certificate cert = X509Certificate.CreateFromCertFile(CertificatePath);
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
                request.Method = "POST";
                request.ClientCertificates.Add(cert);
                request.ContentType = "application/atom+xml";
                request.ContentLength = Encoding.UTF8.GetBytes(requestBody).Length;
                if (RequestHeaders != null)
                {
                    request.Headers.Add(RequestHeaders);
                }

                using (StreamWriter sw = new StreamWriter(request.GetRequestStream()))
                {
                    sw.Write(requestBody);
                    sw.Close();
                }
                resp = request.GetResponse();
                 if (resp != null)
                {
                    HttpWebResponse httpResp = (HttpWebResponse)resp;
                    result.IsOperationSuccessful = httpResp.StatusCode.Equals(HttpStatusCode.Accepted);
                    if (resp.Headers["x-ms-request-id"] != null)
                    {
                        result.RequestID = resp.Headers["x-ms-request-id"];
                    }
                }
            }
            catch (WebException webEx)
            {
                result.Error = webEx;
                result.IsOperationSuccessful = false;
            }
            catch (Exception excep)
            {
                result.Error = excep;
                result.IsOperationSuccessful = false;
            }
            finally
            {
                if (resp != null) { resp.Close(); }
                resp = null;
            }
            return result;
        
        }
    }
}
